package cn.microservices.cloud.common.mybtis.core.util;

import cn.microservices.cloud.common.mybtis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 快速生成
 * </p>
 *
 * @author HYF
 */
public class FastAutoGenerator {


    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql://192.168.10.10:3306/microservices?useOldAliasMetadataBehavior=true&useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456");

    /**
     * 执行 run
     */
    public static void main(String[] args) throws SQLException {
        com.baomidou.mybatisplus.generator.FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig((scanner,builder) -> {
                    builder.author("hyf")
//                    builder.author(scanner.apply("请输入作者名称？"))
                            .fileOverride()
//                        .outputDir("~/Desktop/code/microservices/microservicescloud/microservices-cloud-commons/microservices-cloud-commons-mybatis")
                        .commentDate("yyyy-MM-dd HH:mm:ss").build();})
                // 全局配置
                // 包配置
                .packageConfig((scanner,builder) -> {
                    builder.parent("cn.microservices.cloud")
                            .moduleName("system")
//                            .moduleName(scanner.apply("请输入模块名？"))
                            .entity("dao.dataobject")
                            .service("api.service")
                            .serviceImpl("service.impl")
                            .mapper("dao.mapper")
                            .controller("web.controller")
//                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "~/Desktop/code/microservices/microservicescloud/microservices-cloud-commons/microservices-cloud-commons-mybatis/src/main/resources/mybatis"))
                    ;
                })
                // 策略配置
                .strategyConfig((scanner,builder) -> {
                    builder.addInclude("sys_employee")
//                    builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                        .addTablePrefix("sys_","con_","crm_","dic_","dsg_","fur_","prd_","pur_","sto_","sup_")
                        .entityBuilder()
                        .superClass(BaseDO.class)
                        .disableSerialVersionUID()
                        .enableLombok()
                        .enableTableFieldAnnotation()
                        .addSuperEntityColumns("insert_time","update_time","available_flag","insert_user","update_user","id")
//                        .formatFileName("%sDO")
                ;
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
